package wangn;

public class Main {

    //if you want to use your testcase please change the following variables
    private static boolean hasAssignedFile = false;
    private static String inputFileName="rb2.in";//files should be put in final_redblack_data
    private static String outputFileName="rb2.out";

    private static boolean testSpeed = false;//开启后执行测试时间复杂度
    //***********************************//

    private static RedBlackTree redBlackTree;

    public static void main(final String[] args){

        if (testSpeed){
            redBlackTree = new RedBlackTree();
            int upbound = 10000000;//一千万，在我的笔记本上用时4s
            long start = System.currentTimeMillis();
            for (int i = 0; i < upbound; i++){
                redBlackTree.insert(i);
            }
            System.out.println("执行用时："+(System.currentTimeMillis()-start)/1000);
            new Helper().writeOutFile(redBlackTree,"testspeed.out");
            return;
        }

        if (hasAssignedFile){
            redBlackTree = new RedBlackTree();
            doAndCheck();
        }else{
            while (fileIdx<10){
                inputFileName="rb"+fileIdx+".in";
                outputFileName="rb"+fileIdx+".out";
                redBlackTree = new RedBlackTree();
                if (!doAndCheck())return;
                fileIdx++;
            }
        }
    }

    private static int fileIdx=0;//0...9

    /**
     *
     * @return 仅当红黑树输出与期望不一样且不满足颜色和高度约束时候才返回false，仅仅输出不一致只会打印相应信息
     */
    private static boolean doAndCheck(){

        Helper helper = new Helper();

        String[] inputArr = helper.getStringFromFile(inputFileName).split("/n");
        String[] outputArr = helper.getStringFromFile(outputFileName).split("/n");

        int lines = Integer.parseInt(inputArr[0]);
        int i = 0;
        final String INPUT = "a";
        final String DELETE = "d";
        while(i++<lines){
            String line=inputArr[i];
            String cmd = line.split(" ")[0];
            int value = Integer.parseInt(line.split(" ")[1]);
            if (INPUT.equals(cmd)){
                redBlackTree.insert(value);
            }else if (DELETE.equals(cmd)){
                redBlackTree.delete(value);
            }else{
                throw new RuntimeException("unknown command："+cmd);
            }

            //执行检查
            String answer=outputArr[i-1];
            String myAnswer = redBlackTree.toString();
            boolean same=answer.equals(myAnswer);
            helper.writeOutFile(redBlackTree,outputFileName);

            boolean checkColor = redBlackTree.checkColor();
            int blackHeight = redBlackTree.getBlackHeight();

            if (!same){
                System.out.println("File "+fileIdx+" command "+i+" difference found");
                if (!checkColor || blackHeight==-1){
                    System.out.println("###bug found###");
                    System.out.println("#CheckColor: "+checkColor);
                    System.out.println("#getBlackHeight: "+blackHeight);
                    return false;
                }
            }
        }
        return true;
    }
}
